const reg = /^\/@modules\//;
const path = require('path');
const fs = require('fs').promises

function moduleResolvePlugin({app,root}){
    app.use(async (ctx,next)=>{

        // 如果没有匹配到 /@modules/vue 就往下执行即可
        if(!reg.test(ctx.path)){
            return next();
        }
        const id = ctx.path.replace(reg,'');

        let mapping = {
            vue:path.resolve(root,'node_modules','@vue/runtime-dom/dist/runtime-dom.esm-browser.js'),
        }
        const content = await fs.readFile(mapping[id],'utf8');
        ctx.type = 'js'; // 返回的文件是js
        ctx.body = content;
    })
}


exports.moduleResolvePlugin = moduleResolvePlugin